package com.byedu.course.controller;

import com.byedu.course.*;
import com.byedu.domain.course.vo.*;
import com.byedu.domain.course.UserLibraryDO;
import com.byedu.domain.course.UserQuestionPost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/question")
public class QuestionController {

    protected Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserLibraryService userLibraryService;

    @Autowired
    private UserQuestionService userQuestionService;

    /**
     * 获取考题
     *
     * @param libraryId
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public List<QuestionVO> getQuestionsByLibraryIdlibraryItemId(@RequestParam(value = "userId") String userId, @RequestParam Integer libraryId) {
        return questionService.listQuestions(libraryId, userId);
    }

    /**
     * 获取考题
     *
     * @param libraryId
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listNoAnalysis/{userId}/{libraryId}", method = {RequestMethod.GET})
    public List<QuestionOnlyVO> getQuestionOnlysByLibraryId(@PathVariable Integer libraryId, @PathVariable String userId) {
        return questionService.listQuestionOnlys(libraryId, userId);
    }

    /**
     * 获取解析
     *
     * @param libraryId
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{userId}/{libraryId}", method = {RequestMethod.GET})
    public List<QuestionVO> getQuestionsAnalysisByLibraryId(@PathVariable Integer libraryId, @PathVariable String userId) {
        List<QuestionVO> ls = questionService.listNewQuestions(libraryId, userId);
//        System.out.println( ls);
        //根据新的逻辑更改了新的接口
        return ls;
    }

    /**
     * 获取考题
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/correct/{userId}", method = {RequestMethod.GET})
    public List<QuestionVO> getCorrectQuestionsByLibraryId(@PathVariable String userId) {
        return questionService.listQuestions(userId, true);
    }

    /**
     * 获取考题
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/wrong/{userId}", method = {RequestMethod.GET})
    public List<QuestionVO> getWrongQuestionsByUserId(@PathVariable String userId) {
        return questionService.listQuestions(userId, false);
    }


    /**
     * 获取考题
     *
     * @param courseId
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/wrongbycourse", method = {RequestMethod.GET})
    public List<QuestionVO> wrongbycourse(@RequestParam(value = "userId") String userId, @RequestParam(value = "courseId") Integer courseId) {
//        System.out.println("----"+userId.toString()+courseId);
        return questionService.listErrQuestions(userId,courseId);
    }


    /**
     * 提交答案
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/postquestions", method = {RequestMethod.POST})
    public UserLibraryVO postquestions(@RequestBody UserQuestionPost userQuestionPost) {

        //1、初始化 UserLibraryVO
        //将UserQuestionPost转换为 UserLibraryDO
        String serialno = String.valueOf(System.currentTimeMillis());
        UserLibraryDO userLibraryDO = new UserLibraryDO(userQuestionPost, serialno);
        //转换为VO，并且重新设定UserLibraryVO正确，错题，正确率，核对答题情况
        UserLibraryVO userLibraryVO = new UserLibraryVO(userLibraryDO);
        //2、初始化完成 UserLibraryVO，存储 UserLibraryVO及错题
        int returnStr = userLibraryService.addUserLibraryAndErrQuestion(userLibraryDO);

        //3、去除错题集中做对的题
        questionService.removeErrs(userLibraryDO.getUserId(),userLibraryVO.getAnswerList());

        if (returnStr == 0) {
            logger.error("userQuestionPost:" + userQuestionPost.toString());
        }
//        System.out.println("----" + returnStr);
        if (returnStr > 0) {
            // 查询排行榜
            List<UserLibraryDO> userLibraryDOList = userLibraryService.getUserLibrarysbyLibraryId(userLibraryDO.getLibraryId());
            // 重置排行榜
            for (int i = 0; i < userLibraryDOList.size(); i++) {
                if (serialno.equals(userLibraryDOList.get(i).getSerialno())) {
                    userLibraryVO.setRankNo(i + 1);
                    break;
                }

            }
        }
//        System.out.print(userLibraryVO.toString());
        return userLibraryVO;
    }


    /**
     * 获取个人的试卷答题情况
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getuserLibrarys")
    public List<UserLibraryVO> getuserLibrarys(
     @RequestParam(value = "userId") String userId, @RequestParam(value = "time", required = false) String time) {
        List<UserLibraryDO> userLibraryDOList = userLibraryService.getUserLibrarybyUserId(userId);

        List<UserLibraryVO> lisUserLibraryVO = new ArrayList<UserLibraryVO>();
        for (int i = 0; i < userLibraryDOList.size(); i++) {
            UserLibraryDO userLibraryDO = userLibraryDOList.get(i);
//            LibraryDO libraryDO  = libraryService.getLibraryDOById(userLibraryDO.getLibraryId());
            List<UserLibraryDO> userLibraryDORankList = userLibraryService.getUserLibrarysbyLibraryId(userLibraryDO.getLibraryId());
            UserLibraryVO userLibraryVO = new UserLibraryVO(userLibraryDO);
            for (int j = 0; j < userLibraryDORankList.size(); j++) {
                if (userLibraryDO.getSerialno().equals(userLibraryDORankList.get(j).getSerialno())) {
                    userLibraryVO.setRankNo(i + 1);
                    break;
                }

            }
            lisUserLibraryVO.add(userLibraryVO);
        }

        return lisUserLibraryVO;
    }

    /**
     * 获取个人的试卷答题情况
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getmyerror")
    public List<QuestionVO> getmyerror(
     @RequestParam(value = "userId") String userId, @RequestParam(value = "time", required = false) String time) {


        return questionService.listErrorQuestions(userId, time);
    }

}
